const APP_ID = '720097832232980';
const VERSION = 'v9.0';

export const init = () => {
  window.fbAsyncInit = () => {
    window.FB.init({
      autoLogAppEvents : true,
      appId: APP_ID,
      cookie: true, // Enable cookies to allow the server to access the session.
      xfbml: true, // Parse social plugins on this webpage.
      version: VERSION,
    });
  };
  (function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
};

export const doLogin = () => {
  var uid;
  var accessToken ;
  var expiresIn;
  return new Promise((resolve) => {
    window.FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        // resolve(response.authResponse);
        console.log("facebook--response"+JSON.stringify(response))
        uid = response.authResponse.userID; // 取得 FBID
        accessToken = response.authResponse.accessToken; // 取得 accessToken
        expiresIn = response.authResponse.expiresIn;
        // resolve(response.authResponse);
        window.FB.api('/me?fields=name,first_name,last_name,email', function(user) { // 获取用户自己的FACEBOOK信息
          if(user != null) {
            console.log(user)
            user.head = "https://graph.facebook.com/" + uid + "/picture"; // 头像
            resolve(user)
            var name = user.name;
            var data = "OAuthUid="+uid+"&accessToken="+accessToken+"&expiresIn="+expiresIn+"&name="+name;
          }
        });
      } else {
        window.FB.login(
          (response) => {
            console.log('login response', response);
            if (response.status === 'connected') {
              uid = response.authResponse.userID; // 取得 FBID
              accessToken = response.authResponse.accessToken; // 取得 accessToken
              expiresIn = response.authResponse.expiresIn;
              // resolve(response.authResponse);
              window.FB.api('/me?fields=name,first_name,last_name,email', function(user) { // 获取用户自己的FACEBOOK信息
                if(user != null) {
                  console.log(user)
                  user.head = "https://graph.facebook.com/" + uid + "/picture"; // 头像
                  resolve(user)
                  var name = user.name;
                  var data = "OAuthUid="+uid+"&accessToken="+accessToken+"&expiresIn="+expiresIn+"&name="+name;
                }
              });
              console.log("facebook--response"+JSON.stringify(response))
            }
          },
          { scope: 'user_website,friends_website,user_birthday,friends_birthday,user_hometown,friends_hometown,user_friends,public_profile,email,read_stream,user_photos,user_videos,publish_stream,user_education_history,friends_education_history,user_location,friends_location,user_work_history,friends_work_history' },
        );
      }
    });
  });
};
export const facebookShare = (params) => {
  // return new Promise((resolve) => {
  //       window.FB.ui(
  //         {
  //           method: "share",
  //           mobile_iframe: true,
  //           href: "www.vivebo.com"
  //         },
  //         function(response) {
  //          resolve(response)
  //         }
  //       );
  //       });
};
